package com.hitalkie.teacher.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.hitalkie.teacher.tx.Constants;
import com.hitalkie.teacher.tx.MySelfInfo;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

/**
 * Date 24/09/2016
 *
 * @author charliema
 */

public class BaseActivity extends AppCompatActivity {

    //    public static final String TAG = BaseActivity.class.getSimpleName() ;
    public final String TAG = getClass().getSimpleName();

    public AppCompatActivity getActivity() {
        return this;
    }

    private BroadcastReceiver broadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(Constants.BD_EXIT_APP)) {
                    Log.d("BaseActivity", "LogConstants.ACTION_HOST_KICK +" + MySelfInfo.getInstance().getId() + " on force off line");
                    finish();
                }
            }
        };

        IntentFilter filter = new IntentFilter();
        filter.addAction(Constants.BD_EXIT_APP);

        registerReceiver(broadcastReceiver, filter);

        PushAgent.getInstance(this).onAppStart();


//        // language
//        TalkieApplication.setLocale(TalkieApplication.getLocale());
    }

    @Override
    protected void onResume() {
        super.onResume();
//        MobclickAgent.onPageStart(TAG); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写。"SplashScreen"为页面名称，可自定义)
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
//        MobclickAgent.onPageEnd(TAG); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息。"SplashScreen"为页面名称，可自定义
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        try {
            unregisterReceiver(broadcastReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }
}
